#include <string.h>

#include "gl_def.h"
#include "device_param_init.h"
#include "generic_demand_callback.h"
#include "generic_dynamic_server.h"
#include "generic_event_server.h"

static void *upgrade_test(void *inArg, void *outArg,void *usrArg);
static void *restart_test(void *inArg, void *outArg,void *usrArg);
static void *getDeviceSolarPowerProperties_test(void *inArg, void *outArg,void *usrArg);
static void *getDeviceLogList_test(void *inArg, void *outArg,void *usrArg);
static void *getDeviceLogFile_test(void *inArg, void *outArg,void *usrArg);

int basic_server_init_test() {
    StartTesting("基本信息初始化");
    // 1. 初始化设备参数
    int ret = phyModel_device_paramInit();
    if (ret != IOTC_success) { 
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return -1; 
    } 
    EndTesting("基本信息初始化");

    // 通用命令注册回调函数
    StartTesting("通用命令注册回调函数");
    IOT_comm_demand_funcCallback genericDemandCallback = {0};
    // 注册升级回调功能
    genericDemandCallback.funcCallback = upgrade_test;
    genericDemandCallback.usrArg = NULL;
    if((ret = generic_demand_callback_register(GDEMAND_CALLBACK_TYPE_UPGRADE,&genericDemandCallback)) != IOTC_success){
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return -1; 
    }
    // 注册重启命令功能
    genericDemandCallback.funcCallback = restart_test;
    genericDemandCallback.usrArg = NULL;
    if((ret = generic_demand_callback_register(GDEMAND_CALLBACK_TYPE_ACTION_RESTART,&genericDemandCallback)) != IOTC_success){
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return -1; 
    }

    genericDemandCallback.funcCallback = getDeviceSolarPowerProperties_test;
    genericDemandCallback.usrArg = NULL;
    if((ret = generic_demand_callback_register(GDEMAND_CALLBACK_TYPE_GET_SOLARPOWER,&genericDemandCallback)) != IOTC_success){
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return -1; 
    }

    genericDemandCallback.funcCallback = getDeviceLogList_test;
    genericDemandCallback.usrArg = NULL;
    if((ret = generic_demand_callback_register(GDEMAND_CALLBACK_TYPE_GET_DEVICELOGLIST,&genericDemandCallback)) != IOTC_success){
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return -1; 
    }

    genericDemandCallback.funcCallback = getDeviceLogFile_test;
    genericDemandCallback.usrArg = NULL;
    if((ret = generic_demand_callback_register(GDEMAND_CALLBACK_TYPE_GET_DEVICELOGFILE,&genericDemandCallback)) != IOTC_success){
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return -1; 
    }


    EndTesting("通用命令注册回调函数");
    return 0;
}

int base_server_test() {
    // 上报基本信息
    StartTesting("基本信息上报");

    EndTesting("基本信息上报");

    // 通用技术参数上报
    StartTesting("通用技术参数上报");

    EndTesting("通用技术参数上报");
    return 0;
}

static void *upgrade_test(void *inArg, void *outArg,void *usrArg) {
    // 获取收到的命令
    printf("===get %s command===\n", __FUNCTION__);
    return NULL;
}


static void *restart_test(void *inArg, void *outArg,void *usrArg) {
    // 获取收到的命令
    printf("===get %s command===\n", __FUNCTION__);
    GDemand_actionRestart_inParam *inparam = (GDemand_actionRestart_inParam *)inArg;
    GDemand_actionRestart_outParam *outparam = (GDemand_actionRestart_outParam *)outArg;

    outparam->rltCode = 200;
    return NULL;
    // 函数返回后过几秒后，再重启设备，保证应答数据能上送到平台
}

static void *getDeviceSolarPowerProperties_test(void *inArg, void *outArg,void *usrArg) {
    // 获取收到的命令
    printf("===get %s command===\n", __FUNCTION__);
    
    GDemand_getDeviceSolarPowerProperties_outParam *outparam = (GDemand_getDeviceSolarPowerProperties_outParam *)outArg;
    
    outparam->info.number = 1;
    outparam->info.solarPaneVoltage = 1.0;
    outparam->info.solarPaneCurrent = 1.0;
    outparam->info.batteryVoltage = 1.0;
    outparam->info.batteryCurrent = 1.0;
    outparam->info.loadVoltage = 1.0;
    outparam->info.loadCurrent = 1.0;
    outparam->info.remainBatteryPower = 50;
    outparam->info.batteryTemperature = 25.1;
    outparam->info.batteryChargeStatus = 1;
    return NULL;
}


static void *getDeviceLogList_test(void *inArg, void *outArg,void *usrArg){
    printf("===get %s command===\n", __FUNCTION__);
    
    return NULL;
}

static void *getDeviceLogFile_test(void *inArg, void *outArg,void *usrArg){
    printf("===get %s command===\n", __FUNCTION__);
    GDemand_getDeviceLogFile_inParam *inparam = (GDemand_getDeviceLogFile_inParam*)inArg;
    GDemand_getDeviceLogFile_outParam *outparam = (GDemand_getDeviceLogFile_outParam*)outArg;

    printf("type = %d\n", inparam->type);
    printf("logFileName = %s\n", inparam->logFileName);

    // 上送文件

    // 应答
    outparam->rltCode = 200;

    return NULL;
}


int solarPowerServerInfo_test(void)
{
    solarPowerServerInfo param;
    param.number = 1;
    param.solarPaneVoltage = 1.0;
    param.solarPaneCurrent = 1.0;
    param.batteryVoltage = 1.0;
    param.batteryCurrent = 1.0;
    param.loadVoltage = 1.0;
    param.loadCurrent = 1.0;
    param.remainBatteryPower = 50;
    param.batteryTemperature = 25.1;
    param.batteryChargeStatus = 1;
    
    int ret = phyModel_basic_solarPowerInfoUpload(&param);
    if (ret != IOTC_success) { 
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return ret; 
    } 
    return 0;
}

// 设备定位数据上报
int longitudeAndLatitudeInfoUpload_test(void)
{
    longitudeAndLatitudeInfo param;
    memset(&param,0,sizeof(longitudeAndLatitudeInfo));
    param.positioningSystemInfo = 1;
    param.gwLongitude = 23.123456;
    param.gwLatitude = 113.123456;
    param.gwAltitude = 23;
    
    int ret = phyModel_basic_longitudeAndLatitudeInfoUpload(&param);
    if (ret != IOTC_success) { 
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return ret; 
    } 
    return 0;
}


int commCardInfo_test(void){
    commCardInfo param = {0};
    strcpy(param.ICCID,"123456789");
    strcpy(param.ip,"10.10.10.10");
    strcpy(param.version,"v1.01.1");
    strcpy(param.IMEI,"987654321");
    strcpy(param.IMSI,"678909876");
    strcpy(param.APN,"SWKJ");
    param.CSQ = 90;
    param.CREG = 1;
    int ret =  phyModel_basic_commCardInfo(&param);
    if (ret != IOTC_success) { 
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return ret; 
    } 
    return 0;
}


int deviceTrafficInfoUpload_test(void){
    deviceUseTrafficInfo param;
    memset(&param,0,sizeof(deviceUseTrafficInfo));

    param.dayUseTraffic = 10.3;
    param.monUseTraffic = 1002.1;
    param.monRemainTraffic = 222.89;

    int ret = phyModel_basic_deviceTrafficInfoUpload(&param);
    if (ret != IOTC_success) { 
        IOT_errorInfoRecord(ret,__FILE__,__LINE__); 
        return ret; 
    } 
    return 0;
}

int deviceSleepStatusUpload_test(void){

    return 0;
}

int deviceHeartbeatValueUpload_test(void){

    return 0;
}


int deviceFaultAlarmEventUpload_test(void){
    return 0;
}